<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
		<link rel="stylesheet" type="text/css" href="footer.css" />
		<link rel="stylesheet" href="../../css/lystyle.css" />
		<style type="text/css">
			/*修改底部字体颜色*/
			
			.foot:nth-child(2) {
				color: #e83023;
			}
			
			.ly-line {
				margin: 5px 0.69444rem;
				height: 1px;
				background: #DEDEDE;
			}
			
			.items-wrapper {
				width: 100%;
				margin: 0 auto;
			}
			
			.item {
				width: 100%;
				overflow: hidden;
				padding-bottom: 4px;
				border-bottom: solid #cccccc 1px;
				background-color: #FFFFFF;
			}
			
			.item-title {
				display: inline-block;
				/*width: 0.9333rem;*/
				height: 0.8rem;
				line-height: 0.8rem;
				text-align: center;
				font-size: 0.4267rem;
			}
			
			.item-pic {
				margin-top: 0.1067rem;
				margin-bottom: 0.1067rem;
				width: 100%;
				height: 4.96rem;
				border: 0;
			}
			
			.icon {
				width: 0.32rem;
				height: 0.32rem;
				margin-right: 0.1067rem;
			}
			
			.address,
			.present {
				line-height: 0.8rem;
				font-size: 0.32rem;
				margin:0;
			}
			
			.address {
				color: #555;
			}
			
			.present {
				color: #555;
			}
			
			.item {
				border: 1px solid #CCCCCC;
				margin-bottom: 0.277777rem;
				padding: 15px;
			}
			
			.liebiao {
				outline: none;
				border: 0;
			}
			
			#ly-nav a {
				padding-top: 0.2rem;
				padding-bottom: 0.1rem;
			}
			
			#ly-nav a p {
				margin-bottom: 0;
			}
			
			#ly-nav a:hover {
				color: #e83023;
			}
		</style>
		<script src="../../js/flexible.min.js" type="text/javascript" charset="utf-8"></script>
	</head>

	<body>
		<div id="wrap">
			<div id="main">
				<div id="header">
					<a id="headImg" href="javascript:history.go(-1)"><img class="headImg" src="../../images/12.png" /></a>
					<p id="headTitle">我的收藏</p>
				</div>
				<div id="ly-nav" class="row">
					<a id="orderAll" class="ly-nav ly-nav-active col-xs-6 col-sm-6">
						<select id="liebiao" class="ly-font16 liebiao" name="">
							<option id="xuanzhong" value="全部" selected="selected">全部</option>

						</select>
					</a>
					<a id="orderToPay"   class="ly-nav col-xs-6 col-sm-6">
						<p class="ly-font16" id="juli" >距离</p>
					</a>
				</div>
				<!--内容-->
				<div id="content">
					<!--全部-->
					<div class="items-wrapper">

					</div>
				</div>
			</div>
		</div>

	</body>
	<script src="../../js/jquery-2.2.3.min.js"></script>
	<script src="../../../common/js/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script src="../../js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/handlebars-v2.0.0.js" type="text/javascript" charset="utf-8"></script>
	<script id="table_template1" type="text/x-handlebars-template">

	</script>
	<script type="text/javascript">
		var tokenReztApp = $.cookie("tokenReztApp");
		if(!tokenReztApp) {
			window.location.href = "../login/login/login.html";
		}
		$(function() {
			currentyf();
		});

		//用户信息
		var user, role, goodsId;
		//获取当前会员信息
		function currentyf() {
			$.ajax({
				cache: false,
				type: "GET",
				url: "http://47.104.13.109/member-service/auth/current?token=" + tokenReztApp,
				dataType: "JSON",
				async: false,
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$.removeCookie('tokenReztApp', {
						expires: 7,
						path: '/'
					});
					$.removeCookie("tokenRh", {
						expires: 7,
						path: '/'
					});
					$.removeCookie('memberId', {
						expires: 7,
						path: '/'
					});
					$.removeCookie('roleId', {
						expires: 7,
						path: '/'
					});
					window.location.href = "../login/login/login.html";
				},
				success: function(data) {
					user = data.loginName;
					role = data.roleList[0].id; //角色
					goodsId = data.id;

				}
			});
		}
		$(document).ready(function() {
			console.log($.cookie("tokenRh"));
			$(".ly-nav").click(function() {
				$('.ly-nav').removeClass('ly-nav-active');
				$(this).addClass('ly-nav-active');
			});
			$.ajax({
				type: "get",
				url: "http://47.104.13.109/member-service/member/mine/collect?pageNum=1&pageSize=100&token=" + $.cookie("tokenRh"),
				async: true,
				success: function(data) {
					console.log(data)
					var str = ''
					for(var i = 0; i < data.list.length; i++) {
						$.ajax({
							type: "get",
							url: "http://47.104.13.109/mall-service/shop/search?shopId=" + data.list[i].shopId,
							async: true,
							success: function(data1) {
								console.log(data1)
								if(data1.shopId != null) {
									str += '<div class="item">' +
										'<span class="item-title">' + data1.shopName + '</span>' +
										'<a href="../coalition/commodity_details/commodity_details.html?shopId=' + data1.shopId + '"><img class="item-pic" src="' + data1.shopPic + '" /></a>' +
										'<p class="address"><img class="icon" src="../../images/coalltion/newadress.png">' + data1.shopAdd + 
										'<a class="dianzan" data-a="' + data1.shopId + '"><img class="icon ly-floatri" style="margin-top: 3px;width: 0.5rem;height: 0.5rem;" src="../../images/coalltion/shouchang.png" /></a>' +
										'</p>' +
										'<p class="present"><img class="icon" src="../../images/coalltion/08.png">' + data1.shopSign + '</p>' +
										'</div>'
									$('.items-wrapper').html(str);
								}
							}
						});
					}
				}
			})
			//获取分类列表
			$.ajax({
				type: "get",
				url: "http://47.104.13.109:80/mall-service/lolcart/oneList",
				async: true,
				success: function(data) {
					$.each(data, function(idx, item) {
						$("#liebiao").append('<option value="' + item.name + '">' + item.name + '</option>')
					});

				},
				error: function(error) {
					alert("查询分类列表失败")
				}
			});
			//根据json某字段排序
			function jsonSort(array, field, reverse) {
				//数组长度小于2 或 没有指定排序字段 或 不是json格式数据
				if(array.length < 2 || !field || typeof array[0] !== "object") return array;
				//数字类型排序
				if(typeof array[0][field] === "number") {
					array.sort(function(x, y) {
						return x[field] - y[field]
					});
				}
				//字符串类型排序
				if(typeof array[0][field] === "string") {
					array.sort(function(x, y) {
						return x[field].localeCompare(y[field])
					});
				}
				//倒序
				if(reverse) {
					array.reverse();
				}
				return array;
			}

			//切换类型
			$(document).on('change', '#liebiao', function() {
				this_ = $(this);
				$('.ly-nav').removeClass('ly-nav-active');
				$(this).addClass('ly-nav-active');
				console.log(this_.val())
				$.ajax({
					type: "get",
					url: "http://47.104.13.109/member-service/member/mine/collect?pageNum=1&pageSize=100&token=" + $.cookie("tokenRh"),
					async: true,
					success: function(data) {
						console.log(data)
						var str = ''
						for(var i = 0; i < data.list.length; i++) {
							$.ajax({
								type: "get",
								url: "http://47.104.13.109/mall-service/shop/search?shopId=" + data.list[i].shopId,
								async: true,
								success: function(data1) {
									console.log(data1)
									$('.items-wrapper').html('');
									if(data1.shopId != null && (this_.val() == data1.catName || this_.val() == '全部')) {
										str += '<div class="item">' +
											'<span class="item-title">' + data1.shopName + '</span>' +
											'<a href="../coalition/commodity_details/commodity_details.html?shopId=' + data1.shopId + '"><img class="item-pic" src="' + data1.shopPic + '" /></a>' +
											'<p class="address"><img class="icon" src="../../images/coalltion/07.png">' + data1.shopAdd +
											'<a class="dianzan" data-a="' + data1.shopId + '"><img class="icon ly-floatri" src="../../images/coalltion/shouchang.png" /></a>' +
											'</p>' +
											'<p class="present"><img class="icon" src="../../images/coalltion/08.png">' + data1.shopSign + '</p>' +
											'</div>'
										$('.items-wrapper').html(str);
									}
								}
							});
						}
					}
				})

			})
			var str = '';
			var str1 = [];
			var str2 = {};
			var str3;
			$(function(){
				$.ajax({
					type: "get",
					url: "http://47.104.13.109/member-service/member/mine/collect?pageNum=1&pageSize=100&token=" + $.cookie("tokenRh"),
					async: true,
					success: function(data) {
						for (var i = 0;i < data.list.length;i++) {
							str2.index = i;
							$.ajax({
								type: "get",
								url: "http://47.104.13.109/mall-service/shop/search?shopId=" + data.list[i].shopId,
								async: true,
								success: function(data1) {
									str1.push(data1)
									str3 = jsonSort(str1,'distance');

								},
								error:function(error){
									alert('查询收藏商品时出错')
								}
								})
						}
						
					}
				})
			})
			//切换距离
			$(document).on('click', '#juli', function() {
				var str4 = '';
				$("#liebiao").removeClass('ly-nav-active');
				$(this).addClass('ly-nav-active');
				$("#liebiao").find("option:selected").text("全部"); 
					for (var u = 0;u < str3.length;u++) {
							if(str3[u].shopId != null) {
								str4 += '<div class="item">' +
										'<span class="item-title">' + str3[u].shopName + '</span>' +
										'<a href="../coalition/commodity_details/commodity_details.html?shopId=' + str3[u].shopId + '"><img class="item-pic" src="' + str3[u].shopPic + '" /></a>' +
										'<p class="address"><img class="icon" src="../../images/coalltion/07.png">' + str3[u].shopAdd +
										'<a class="dianzan" data-a="' + str3[u].shopId + '"><img class="icon ly-floatri" src="../../images/coalltion/收-藏.png" /></a>' +
										'</p>' +
										'<p class="present"><img class="icon" src="../../images/coalltion/08.png">' + str3[u].shopSign + '</p>' +
										'</div>'
									$('.items-wrapper').html(str4);
								}
					}

			})
			$(document).on('click', '.dianzan', function() {
				var shopId = $(this).attr('data-a')
				console.log(shopId)
				var this_ = $(this).children();
				$.ajax({
					type: "get",
					url: "http://47.104.13.109/member-service/member/client/collect/check",
					async: true,
					data: {
						shop_id: shopId,
						token: $.cookie('tokenRh')
					},
					success: function(bol) {
						if(bol) {
							//删除收藏
							$.ajax({
								type: "delete",
								url: 'http://47.104.13.109/member-service/member/mine/collect/' + shopId + '?token=' + $.cookie("tokenRh"),
								async: true,
								success: function(res) {
									console.log(res);
									if(res) {
										console.log($(this).children())
										this_.attr('src', '../../images/coalltion/weishouchang.png');
									}
								},
								error: function(error) {
									alert("出现错误，请联系系统管理员");
								}
							});
						} else {
							//添加收藏
							$.ajax({
								type: "post",
								url: 'http://47.104.13.109/member-service/member/mine/collect/' + shopId + '?token=' + $.cookie("tokenRh"),
								async: true,
								success: function(res1) {
									console.log(res1);
									if(res1) {
										this_.attr('src', '../../images/coalltion/shouchang.png');
									}
								},
								error: function(error) {
									alert("出现错误，请联系系统管理员");
								}
							});
						}
					},
					error: function(error) {
						alert("出现错误，请联系系统管理员");
					}
				});

			})

		});
	</script>

</html>